這是我第一次在it邦發文章,主要是做一下筆記,順便和大家技術交流,請各位指教
首先是IFTTT,說明請參照IFTTT 發送 LINE 訊息通知
再來是java的類別部分
JMaker.java
package javaapplication4;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class JMaker {
    private String eventName;
    private String key;
    public JMaker(String eventName, String key) {
        this.eventName = eventName;
        this.key = key;
    }
    public void trigger() throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        httpClient.execute(request);
    }
    public void trigger(String values) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        StringEntity params = new StringEntity(buildJson(values), "UTF-8");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        httpClient.execute(request);
    }
    //2018-07-25 確認是否發送
    public void trigger(String values, int state) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        if (state == 0) {
            StringEntity params = new StringEntity(buildJson(values), "UTF-8");
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            httpClient.execute(request);
        } else {
            httpClient.execute(request);
        }
    }
    private String buildJson(String values) throws UnsupportedEncodingException {
        String json = "{";
        json += "\"value1\":\"" + values + "\"";
        json += "}";
        System.out.println("傳送 json: " + json);
        return json;
    }
}
最後是使用的部分
main.java
JMaker maker = new JMaker(IFTTT的eventNam, IFTTT的key);
maker.trigger("test");
lib引用的部分
完成的樣子
最後再次讚嘆google大神的偉大,也感謝各位大大無私的分享,在此貢獻小弟一點經驗,回饋大家